package cn.iocoder.yudao.module.water.controller.admin.year;

import cn.iocoder.yudao.module.water.controller.admin.month.vo.ReportMonthRespVO;
import cn.iocoder.yudao.module.water.convert.month.ReportMonthConvert;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.water.controller.admin.year.vo.*;
import cn.iocoder.yudao.module.water.dal.dataobject.year.ReportYearDO;
import cn.iocoder.yudao.module.water.convert.year.ReportYearConvert;
import cn.iocoder.yudao.module.water.service.year.ReportYearService;

@Api(tags = "管理后台 - 年报")
@RestController
@RequestMapping("/water/report-year")
@Validated
public class ReportYearController {

    @Resource
    private ReportYearService reportYearService;

    @PostMapping("/create")
    @ApiOperation("创建年报")
    @PreAuthorize("@ss.hasPermission('water:report-year:create')")
    public CommonResult<Long> createReportYear(@Valid @RequestBody ReportYearCreateReqVO createReqVO) {
        return success(reportYearService.createReportYear(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新年报")
    @PreAuthorize("@ss.hasPermission('water:report-year:update')")
    public CommonResult<Boolean> updateReportYear(@Valid @RequestBody ReportYearUpdateReqVO updateReqVO) {
        reportYearService.updateReportYear(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除年报")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:report-year:delete')")
    public CommonResult<Boolean> deleteReportYear(@RequestParam("id") Long id) {
        reportYearService.deleteReportYear(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得年报")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:report-year:query')")
    public CommonResult<ReportYearRespVO> getReportYear(@RequestParam("id") Long id) {
        ReportYearDO reportYear = reportYearService.getReportYear(id);
        return success(ReportYearConvert.INSTANCE.convert(reportYear));
    }

    @GetMapping("/list")
    @ApiOperation("获得年报列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('water:report-year:query')")
    public CommonResult<List<ReportYearRespVO>> getReportYearList(@RequestParam("ids") Collection<Long> ids) {
        List<ReportYearDO> list = reportYearService.getReportYearList(ids);
        return success(ReportYearConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得年报分页")
    @PreAuthorize("@ss.hasPermission('water:report-year:query')")
    public CommonResult<PageResult<ReportYearRespVO>> getReportYearPage(@Valid ReportYearPageReqVO pageVO) {
        PageResult<ReportYearDO> pageResult = reportYearService.getReportYearPage(pageVO);
        return success(ReportYearConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出年报 Excel")
    @PreAuthorize("@ss.hasPermission('water:report-year:export')")
    @OperateLog(type = EXPORT)
    public void exportReportYearExcel(@Valid ReportYearExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ReportYearDO> list = reportYearService.getReportYearList(exportReqVO);
        // 导出 Excel
        List<ReportYearExcelVO> datas = ReportYearConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "年报.xls", "数据", ReportYearExcelVO.class, datas);
    }

    @GetMapping("/get-one-year-report")
    @ApiOperation("获得一年年报")
    @ApiImplicitParam(name = "date", value = "时间", required = true, example = "1024", dataTypeClass = Date.class)
    @PreAuthorize("@ss.hasPermission('water:report-month:query')")
    public CommonResult<List<ReportYearRespVO>> getReportYear(@RequestParam("date") String date) {
        List list = reportYearService.getReportOneYearList(date);
        return success(ReportYearConvert.INSTANCE.convertList(list));
    }

}
